Namespace DirectoryHelper

    Public Class ADHelperConfig
        Inherits System.Configuration.ConfigurationSection

        <Configuration.ConfigurationProperty("AD_Host", IsRequired:=False)> _
        Public ReadOnly Property AD_Host() As String
            Get
                Return TryCast(Me("AD_Host"), String)
            End Get
        End Property

        <Configuration.ConfigurationProperty("AD_Root", IsRequired:=False)> _
        Public ReadOnly Property AD_Root() As String
            Get
                Return TryCast(Me("AD_Root"), String)
            End Get
        End Property

        <Configuration.ConfigurationProperty("AD_User", IsRequired:=False)> _
        Public ReadOnly Property AD_User() As String
            Get
                Return TryCast(Me("AD_User"), String)
            End Get
        End Property

        <Configuration.ConfigurationProperty("AD_Password", IsRequired:=False)> _
        Public ReadOnly Property AD_Password() As String
            Get
                Return TryCast(Me("AD_Password"), String)
            End Get
        End Property

        <Configuration.ConfigurationProperty("ImageURLs", IsRequired:=False)> _
        Public ReadOnly Property ImageURLS() As ImageURLStringsCollection
            Get
                Return TryCast(Me("ImageURLs"), ImageURLStringsCollection)
            End Get
        End Property

        Public Shared Function GetConfig() As ADHelperConfig
            Return TryCast(Configuration.ConfigurationManager.GetSection("ADHelperConfig"), ADHelperConfig)
        End Function

        Public Shared Function GetImageURLs() As ImageURLStringsCollection
            Dim ImageURLs As ImageURLStringsCollection = Nothing
            Dim ConfigSection As ADHelperConfig = TryCast(Configuration.ConfigurationManager.GetSection("ADHelperConfig"), ADHelperConfig)
            If Not (ConfigSection Is Nothing) Then
                ImageURLs = ConfigSection.ImageURLS
            End If
            Return ImageURLs
        End Function

    End Class

    Public Class ImageURLStrings
        Inherits System.Configuration.ConfigurationElement

        <Configuration.ConfigurationProperty("CategoryName", IsRequired:=False)> _
        Public ReadOnly Property Categoryname() As String
            Get
                Return TryCast(Me("CategoryName"), String)
            End Get
        End Property

        <Configuration.ConfigurationProperty("URL", IsRequired:=False)> _
        Public ReadOnly Property URL() As String
            Get
                Return TryCast(Me("URL"), String)
            End Get
        End Property

    End Class

    Public Class ImageURLStringsCollection
        Inherits Configuration.ConfigurationElementCollection

        Default Public Overloads Property Item(ByVal index As Integer) As ImageURLStrings
            Get
                Return TryCast(MyBase.BaseGet(index), ImageURLStrings)
            End Get
            Set(ByVal value As ImageURLStrings)
                If Not (MyBase.BaseGet(index) Is Nothing) Then
                    MyBase.BaseRemoveAt(index)
                End If
                Me.BaseAdd(index, value)
            End Set
        End Property

        Protected Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement
            Return New ImageURLStrings
        End Function

        Protected Overrides Function GetElementKey(ByVal element As System.Configuration.ConfigurationElement) As Object
            Return DirectCast(element, ImageURLStrings).Categoryname
        End Function
    End Class

End Namespace